/*
 * Licensed to the Apache Software Foundation (ASF) under one or more
 * contributor license agreements. See the NOTICE file distributed with
 * this work for additional information regarding copyright ownership.
 * The ASF licenses this file to You under the Apache license, Version 2.0
 * (the "License"); you may not use this file except in compliance with
 * the License. You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the license for the specific language governing permissions and
 * limitations under the license.
 */
package com.hsh;

import org.apache.logging.log4j.core.LoggerContext;
import org.apache.logging.log4j.core.config.*;
import org.apache.logging.log4j.core.config.composite.CompositeConfiguration;
import org.apache.logging.log4j.core.config.plugins.Plugin;
import org.apache.logging.log4j.core.config.properties.PropertiesConfiguration;
import org.apache.logging.log4j.core.config.properties.PropertiesConfigurationBuilder;
import org.apache.logging.log4j.core.config.xml.XmlConfiguration;

import java.io.IOException;
import java.io.InputStream;
import java.util.ArrayList;
import java.util.List;
import java.util.Properties;


/**
 * Description: 定义log4j2读取配置文件插件
 *
 * @author 冯东宝（dongbao.feng@ucarinc.com）
 * @date 2019年03月13日18:28:31
 * @since 1.0
 */
@Plugin(name = "UcarincPropertiesConfigurationFactory", category = ConfigurationFactory.CATEGORY)
@Order(20)
public class UcarincPropertiesConfigurationFactory extends ConfigurationFactory {

    /*
     * log4j2.ucarinc 、log4j2.xml 与 log4j2-ucarinc.xml 的作用
     * log4j2.ucarinc ： 为了兼容log4j2.xml，在本地项目没有配置log4j2_ucarinc.properties文件的情况下，触发ConfigurationFactory
     *                   内容为空，起到触发加载配置中心的作用。
     * log4j2.xml ： 用户使用 log4j2_ucarinc.properties 配置日志时，防止springboot扫描不到日志配置文件，加载默认配置文件log4j2-file.xml
     *
     * log4j2-ucarinc.xml ：用户自定义log4j2.xml 文件时，程序会加载用户自定义的文件，不会加载该jar内的log4j2.xml，此时需要返回默认日志配置
     */

    /**
     * _ucarinc.properties  .ucarinc  后缀的文件出发该 ConfigurationFactory
     * log4j2.ucarinc的内容为空，起到触发加载配置中心的作用。
     *
     * @return
     */
    @Override
    protected String[] getSupportedTypes() {
        return new String[]{"_ucarinc.properties", ".ucarinc"};
    }

    @Override
    public Configuration getConfiguration(final LoggerContext loggerContext, final ConfigurationSource source) {


        final Properties properties = new Properties();
        try (final InputStream configStream = source.getInputStream()) {
            properties.load(configStream);
        } catch (final IOException ioe) {
            throw new ConfigurationException("Unable to load " + source.toString(), ioe);
        }
        PropertiesConfiguration pc = new PropertiesConfigurationBuilder()
                .setConfigurationSource(source)
                .setRootProperties(properties)
                .setLoggerContext(loggerContext)
                .build();


//        ConfigurationSource xmlSource = ConfigurationSource.fromResource("log4j2-async-template.xml", this.getClass().getClassLoader());
//        XmlConfiguration template = new XmlConfiguration(loggerContext, xmlSource);
        List<AbstractConfiguration> list = new ArrayList<>();
        //list.add(createDefaultConfiguration(loggerContext));
        //list.add(template);
        list.add(pc);

        // CompositeConfiguration 后面的覆盖前面的，所以要把配置中心的pConfig 放在最后
        CompositeConfiguration conf = new CompositeConfiguration(list);
        return conf;

    }
}
